import cn.zlj.utils.ESClientUtil;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;

public class EsTest2 {

    @Test
    public void Complex(){
        //获取连接
        TransportClient client = ESClientUtil.getClient();

        SearchRequestBuilder builder = client.prepareSearch("pethome");
        builder.setTypes("pet");
        BoolQueryBuilder bool = QueryBuilders.boolQuery();
        //关键词
        bool.must(QueryBuilders.matchQuery("name","zs张三是你爹"))
        //年龄范围
        .filter(QueryBuilders.rangeQuery("age").gte(60).lte(200));
        //性别
        bool.filter(QueryBuilders.termQuery("sex","0"));

        builder.setQuery(bool);

        builder.setFrom(2);
        builder.setSize(5);
        builder.addSort("age", SortOrder.ASC);
        SearchHits hits = builder.get().getHits();
        System.out.println(hits);
        hits.forEach(System.out::println);
        client.close();

    }
}
